<?php

class CartMode extends SiteMode {
    /** @return CartMode */
    public static function init() {
        return parent::init();
    }

    // 用户购物车列表,暂时只搜索了最基本的信息
    function getList($user_id,$cart_ids = null){
        $data=array(
            'cart C'        =>array('*'),
            'goods G'       =>array('name','type','shop_id','id'=>'goods_id'),
            'goods_attr GA' =>array('name'=>'attr_name','id'=>'attr_id')
        );
        $condition=array(
            'C.user_id'=>$user_id,
            'G.`gopen_id`=C.`gopen_id`',
            'C.`attr_id`=GA.`id`',
            'GA.`gopen_id`=G.`gopen_id`',
            'G.`enable`=1',
            'C.site_id'=>$this->siteId
        );
        if(!empty($cart_ids)){
            $condition[]=$this->db->getIdCondition($cart_ids, "C.id");
        }
        $sql=$this->db->selectSql($data,$condition,null,0,'G.shop_id');
        $r=$this->db->getAll($sql,$user_id);
        if(!empty($r)){
            $g=GoodsMode::init();
            foreach ($r as &$v) {
                $p=$g->getPriceOfGoods($v['gopen_id']);//需要显示出来也可以
                //合适数量的价格
                $v['real_price']=$g->getPriceByNum($p['id'],$v['number']);
                $v['share']=$g->getShareDetail($user_id,$v['gopen_id']);
            }
        }
        return $r;
    }
    // 添加商品到购物车，添加前check一下哦~
    function add($user_id,$gopen_id,$attr_id,$number=1){
        // 检查存在否
        if($this->checkExist($user_id,$gopen_id)) return -1;

        $data['user_id']=$user_id;
        $data['gopen_id']=$gopen_id;
        $data['attr_id']=$attr_id;
        $data['number']=$number;
        $data['site_id']=$this->siteId;
        return $this->db->insert('cart',$data)==1? (int)$this->db->insertId(): -1;
    }

    // 修改购物车内容
    function update($cart_id, $attr_id, $number){
        $sql="UPDATE `cart` SET `number`= ?, `attr_id`=? WHERE `id`=? and site_id=?";
        return $this->db->sqlExec($sql, (int)$number, (int)$attr_id, (int)$cart_id, $this->siteId)==1;
    }
    // 修改购物车内容
    function updateByGopen($user_id, $gopen_id, $attr_id, $number){
        $sql="UPDATE `cart` SET `number`= ?, `attr_id`=? WHERE `gopen_id`=? and `user_id`=? and site_id=?";
        return $this->db->sqlExec($sql, (int)$number, (int)$attr_id, (int)$gopen_id, (int)$user_id, $this->siteId)==1;
    }
    // 检查购物车是否已存在gopen_id,不检查属性
    function checkExist($user_id, $gopen_id){
        $sql="SELECT * FROM `cart` WHERE `user_id`=? AND `gopen_id`=? and site_id=?";
        return $this->db->getExist($sql, (int)$user_id, (int)$gopen_id, $this->siteId);
    }
    // 检查商品数量是否超过库存
    function checkNumValid($attr_id, $number){
        $sql="SELECT `stock` FROM `goods_attr` WHERE `id`=? and site_id=?";
        $attrStock=(int)$this->db->getValue($sql,$attr_id, $this->siteId);
        return $attrStock >= (int)$number;
    }

    // 自动决定添加还是修改
    function modify($user_id, $gopen_id, $attr_id, $number=1){
        return $this->checkExist($user_id,$gopen_id) ?
            $this->updateByGopen($user_id, $gopen_id, $attr_id, $number) :
            $this->add($user_id, $gopen_id, $attr_id, $number);
    }
    // 移出
    function remove($ids){;
        return $this->db->delete('cart',$ids) == count($ids);
    }

    /**
     * @param $user_id
     * @param $gopen_ids  '1,2,3'|array(1,2,3)
     * @return bool
     */
    function removeByGopenIds($user_id,$gopen_ids){
        $user_id=(int)$user_id;
        $sql="SELECT `id` FROM `cart` WHERE `user_id`=$user_id and ".$this->db->getIdCondition($gopen_ids,'gopen_id');
        $ids=$this->db->getAll($sql);
        $idsDealt=array_map(function($v){return $v['id'];},$ids);
        return $this->db->delete('cart',$idsDealt) ==  count($ids);
    }
    function getNumber($user_id){
        $sql="SELECT count(1) FROM `cart` C,goods G WHERE `user_id`=? and G.`gopen_id`=C.`gopen_id` and G.`enable`=1";
        return $this->db->getValue($sql,$user_id);
    }
}